
//卡车类
import {Basebio} from "./basebio";
import g1 from "../sound/g1.wav";
import {a_soldier_actionproperty, truck_actionproperty, truck_actions} from "../commdata";
import {Obj_class} from "./obj_class";
import {ReleaseResource} from "../comm";

export class Truck extends Basebio{

    imgyy:any;
    constructor(_name:string,_live:number,_livelimit:number,_x:number,_y:number,_demage:number,_shootrange:number,_team:number,_speed:number) {
        super();
        this.name = _name;
        this.live = _live;
        this.livelimit = _livelimit;
        this.x = _x;
        this.y = _y;
        this.actioncount = 1;
        this.actionimg = new Image();
        this.currantactions=[];
        this.fun = "l";
        this.demage = _demage;
        this.imgyy=new Image();
        this.imgyy.src=truck_actionproperty.yyimgsrc;
        this.shootrange = _shootrange+Math.floor(Math.random() * (50) + 0);
       // this.sdadio.src = g1;
        this.fireimag = new Image();
        this.team=_team;
        this.actrons = truck_actions;
        this.ratewidth=truck_actionproperty.ratewidth;
        this.rateheight=truck_actionproperty.rateheight;

       // this.sdadio.src=g1;
        this.actionindex = truck_actionproperty.actionindex;
        this.actioncount = truck_actionproperty.actioncount;
        this.speed=_speed;
    }

    actiondraw = (ptctx: any, max_width: number, max_height: number) => {
        if(this.live>0) {
            if (this.fun == "l") {
                this.x = this.x - this.speed;
            } else if (this.fun == "r") {
                this.x = this.x + this.speed;
            } else if (this.fun == "u") {
                this.y = this.y - this.speed;
            } else if (this.fun == "d") {
                this.y = this.y + this.speed;
            } else if (this.fun == "ul") {
                this.x = this.x - this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "ur") {
                this.x = this.x + this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "dl") {
                this.x = this.x - this.speed;
                this.y = this.y + this.speed;
            } else if (this.fun == "dr") {
                this.x = this.x + this.speed;
                this.y = this.y + this.speed;
            } else {
                this.x = this.x + this.speed;
            }

            if (this.x < 0) {
                this.actionindex = 0;
                this.fun = "r";
                this.x = 0;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    // if(this.isfire==true)
                    // {
                    //     //this.sdadio.play();
                    //     tempfun = this.fun + "_f";
                    // }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.x > max_width - this.ratewidth) {
                this.actionindex = 0;
                this.fun = "l";
                this.x = max_width - this.ratewidth;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    // if(this.isfire==true)
                    // {
                    //     //this.sdadio.play();
                    //     tempfun = this.fun + "_f";
                    // }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y < 0) {
                this.actionindex = 0;
                this.y = 0;
                this.fun = "d";
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    // if(this.isfire==true)
                    //     //if(this.fireindex>1) {
                    //     //this.sdadio.play();
                    //     tempfun = this.fun + "_f";
                    // //}
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y > max_height - this.rateheight) {
                this.actionindex = 0;
                this.fun = "u";
                this.y = max_height - this.rateheight;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    // if(this.isfire==true)
                    // {
                    //     //this.sdadio.play();
                    //     tempfun = this.fun + "_f";
                    // }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            }
            this.currantactions = [];
            for (let i = 0; i < this.actrons.length; i++) {
                let tempfun=this.fun;
                // if(this.isfire==true)
                // {
                //     //this.sdadio.play();
                //     tempfun = this.fun + "_f";
                // }
                if (tempfun == this.actrons[i].actiontype) {
                    let temp = this.actrons[i];
                    this.currantactions.push(temp);
                }
            }
            this.actioncount=this.currantactions.length;
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x, this.y, this.ratewidth, this.rateheight
            );
            this.drawtankinfo(ptctx);
            if (this.actionindex < this.actioncount - 1)
                this.actionindex = this.actionindex + 1;
            else
                this.actionindex = 0;
        }
    }

    drawshowdo=(ptctx: any)=>{
        if(this.live>0) {
            //绘制阴影
            ptctx.globalAlpha = 0.4;
            ptctx.drawImage(this.imgyy, 0, 0, this.imgyy.width, this.imgyy.height, this.x, this.y + this.rateheight/2+8, this.ratewidth, this.rateheight/3);
            //ptctx.drawImage(this.imgyy, 0, 0, this.imgyy.width, this.imgyy.height, this.x + 5, this.y + this.rateheight - 15, this.ratewidth - 15, this.rateheight / 2 - 15);
            ptctx.globalAlpha = 1;
        }
    }

    drawtankinfo=(ptctx: any)=>{
        // if(this.name!="") {
        //     ptctx.font = "oblique small-caps bold 10px Arial";
        //     ptctx.fillStyle = "#33FF00";
        //     ptctx.fillText(this.name, this.x, this.y - 20);
        //     ptctx.fillText("生命：" + this.live.toString()+"/"+this.livelimit.toString(), this.x, this.y-10);
        //     ptctx.fillText("杀敌：" + this.killcount.toString(), this.x, this.y);
        //     ptctx.stroke();
        // }
    }

    deaddraw=(ptctx: any)=>{
        if(this.deadfinish==false)
        {
            this.currantactions = [];
            for (let i = 0; i < this.actrons.length; i++) {

                if (this.actrons[i].actiontype == "deadflame") {
                    let temp = this.actrons[i];
                    this.currantactions.push(temp);
                }
            }
            this.actioncount= this.currantactions.length;
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
          //  console.log(this.actionimg.src );
            ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x-10, this.y-10, this.actionimg.width, this.actionimg.height
            );
            if (this.actionindex < this.actioncount - 1)
                this.actionindex = this.actionindex + 1;
            else
            {
                this.actionindex=0;
                this.deadfinish=true;
                ReleaseResource(this);
            }
        }

    }

    setTruckfire=(enemys: Obj_class[])=>{
        let flagnum: number = Math.floor(Math.random() * (100 - 0 + 1)) + 0;
        if(flagnum>55 && flagnum < 95)
        {
            this.isfire=true;
            for (let i = 0; i < enemys.length; i++) {
                //射中机率
                let tmpflag = Math.floor(Math.random() * (70 - 0 + 1)) + 0;
                if (tmpflag > 50) {

                    if (this.isAtacctRange(enemys[i].objval) == true) {
                        //在攻击范围
                        if (enemys[i].objval.live > 0)
                            enemys[i].objval.live = enemys[i].objval.live - this.demage;
                        else {
                            enemys[i].objval.live = 0;
                        }
                        return;
                    }
                }
                // this.sdadio.play();
                // fighter.live = fighter.live - this.demage;
            }
        }
    }
}